# 504. 单词分析
# https://www.lanqiao.cn/problems/504/learning/?page=1&first_category_id=1&second_category_id=3
# Date: 2025/2/15
from collections import defaultdict, Counter


def get_most_frequent_word(word: str):
    mapping = defaultdict(int)
    max_num = 0
    for c in word:
        mapping[c] += 1
        if mapping[c] > max_num:
            max_num += 1
    keys = list(mapping.keys())
    keys.sort(key=lambda x: ord(x))
    for key in keys:
        if mapping[key] == max_num:
            print(key)
            print(mapping[key])
            break


def get_most_frequent_word_cnt(word: str):
    cnt = Counter(word)
    max_count = max(cnt.values())

    # 按ASCII码排序后取第一个最大值
    for c in sorted(cnt):
        if cnt[c] == max_count:
            print(c)
            print(max_count)
            break


if __name__ == '__main__':
    a, b, c = ("lanqiao", "lanqiaoabbb", "ba")
    get_most_frequent_word(a)
    get_most_frequent_word_cnt(a)
    print("=" * 15)
    get_most_frequent_word(b)
    get_most_frequent_word_cnt(b)
    print("=" * 15)
    get_most_frequent_word_cnt(c)
